package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Task;
import com.wxxymaker.edu.roles.AdminRoles;
import com.wxxymaker.edu.roles.IgnoreAuth;
import com.wxxymaker.edu.service.AdminService;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @Author: zhanghuan
 * @Date: 2018/10/13 10:47 PM
 * @DESCRIBE:
 */
@AdminRoles
@Path(value = "/admin", restful = true)
public class AdminHandlerApi {

    @Inject
    private AdminService adminService;

    /**
     * 系统用户信息分页渲染
     *
     * @param page
     * @param size
     * @return
     */
    @IgnoreAuth
    @PostRoute("/systemuser/page")
    public RestResponse allUsers(@Param(defaultValue = "1") int page,
                                 @Param(defaultValue = "10") int size) {
        Page<User> users = adminService.allUsers(page, size);
        if (users.getTotalRows() > 0) {
            return RestResponse.ok(users);
        }
        return RestResponse.fail();
    }

    /**
     * 系统用户信息分页渲染
     *
     * @param page
     * @param size
     * @return
     */
    @IgnoreAuth
    @PostRoute("/systemuser/category")
    public RestResponse categoryUsers(@Param Integer category,
                                      @Param String content,
                                      @Param(defaultValue = "1") int page,
                                      @Param(defaultValue = "10", name = "limit") int size) {
        User user = TokenUtils.currentUser();
        Page<User> users = adminService.search(category, content, page, size, user.getId());
        if (users.getTotalRows() > 0) {
            return RestResponse.ok(users);
        }
        return RestResponse.fail();
    }

    /**
     * @param userId 用户id 用来审核该用户账号
     * @return
     */
    @IgnoreAuth
    @PostRoute("/systemuser/audit")
    public RestResponse audit(@Param int userId) {
        User user = adminService.audit(userId);
        if (user.getAudit().equals(0)) {
            user.setAudit(1);
        } else {
            user.setAudit(0);
        }
        user.update();//小傻逼，不更新
        return RestResponse.ok();
    }

    /**
     * 视频信息分页渲染
     *
     * @param page
     * @param size
     * @return
     */
    @IgnoreAuth
    @PostRoute("/vedioaudit/page")
    public RestResponse vedioAudit(@Param(defaultValue = "1") int page,
                                   @Param(defaultValue = "10", name = "limit") int size) {
        Page<Task> task = adminService.vedioAuidt(page, size);
        if (task != null) {
            return RestResponse.ok(task);
        }
        return RestResponse.fail();
    }


    /**
     * @param tid 任务tid 用来审核视频发布
     * @return
     */
    @IgnoreAuth
    @PostRoute("/vedioaudit/audit")
    public RestResponse vedio(@Param int tid) {
        Task task = adminService.video(tid);
        if (task.getStatus().equals(0)) {
            task.setStatus(1);
        } else {
            task.setStatus(0);
        }
        task.update();
        return RestResponse.ok();

    }


//    /**
//     * 暂时不用
//     * @param userId 用户id 级联删除该用户所有信息
//     * @return
//     */
//
//    @IgnoreAuth
//    @PostRoute("/systemuser/delete")
//    public RestResponse delete(@Param int userId) {
//        User user = adminService.audit(userId);
//        user.delete();
//        return RestResponse.ok(user);
//
//    }
}
